package br.com.sisat.servlet;

import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.text.AttributedString;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

import br.com.sisat.modelo.RelatorioPlanoSaudeDTO;

public class GraphicServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		OutputStream out = response.getOutputStream();
		JFreeChart chart = null; 
		String tipoGrafico = request.getParameter("tipoGrafico");
		if("1".equalsIgnoreCase(tipoGrafico)){
			chart = createGrafico((List<RelatorioPlanoSaudeDTO>) request.getSession().getAttribute("listaPlano"),"Atendimento por Plano de Saude",1);
		}else if("2".equalsIgnoreCase(tipoGrafico)){
			chart = createGrafico((List<RelatorioPlanoSaudeDTO>) request.getSession().getAttribute("listaEspecialidade"),"Atendimento por Especialidade",2);
		}else{
			chart = createGrafico((List<RelatorioPlanoSaudeDTO>) request.getSession().getAttribute("listaTerapeuta"),"Atendimento por Terapeuta",3);
		}
		
		
		BufferedImage imagem = chart.createBufferedImage(500, 300);
		ImageIO.write(imagem, "png", out);
		out.close();
	}

	private JFreeChart createGrafico(List<RelatorioPlanoSaudeDTO>dados, String title, int tipo) {
		DefaultPieDataset piedataset = new DefaultPieDataset();
		for (RelatorioPlanoSaudeDTO dado : dados) {
			dado.setType(tipo);
			piedataset.setValue(" "+dado.getLabel()+ "\n ("+dado.getValue()+") ", dado.getValue());
		}
		
		JFreeChart jfreechart = ChartFactory.createPieChart(title,
				piedataset, false, true, false);
		PiePlot pieplot = (PiePlot) jfreechart.getPlot();
		pieplot.setLabelGenerator(new CustomLabelGenerator());
		return jfreechart;
	}


	static class CustomLabelGenerator implements PieSectionLabelGenerator {

		public String generateSectionLabel(PieDataset piedataset,
				Comparable comparable) {
			String s = null;
			if (piedataset != null)
				s = comparable.toString();
			return s;
		}

		public AttributedString generateAttributedSectionLabel(
				PieDataset piedataset, Comparable comparable) {
			AttributedString attributedstring = null;
			String s = comparable.toString();
			String s1 = s + " : "
					+ String.valueOf(piedataset.getValue(comparable));
			attributedstring = new AttributedString(s1);
			attributedstring.addAttribute(TextAttribute.WEIGHT,
					TextAttribute.WEIGHT_BOLD, 0, s.length() - 1);
			return attributedstring;
		}

		CustomLabelGenerator() {
		}
	}

}
